package com.gotobject.junit.internal.runners.statements;

import com.gotobject.junit.internal.Registry;
import static com.gotobject.junit.internal.Registry.get;
import com.gotobject.junit.internal.runners.model.PerformanceKey;
import com.gotobject.junit.internal.runners.model.Explosion;
import com.gotobject.junit.internal.runners.model.Measure;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

import java.util.List;

/**
 *
 * @author Huascar A. Sanchez (huascar.sanchez@gmail.com)
 */
public class FailOnLoadExplosion extends FailOnExplosion {
    private         Measure             measure;
	private final 	List<Explosion>     cluster;
	private final   Description         currentTestMethod;


    /**
	 * Construct a {@code FailOnLoadExplosion} object for a {@link Statement next statement}
	 * and some {@link com.gotobject.junit.internal.runners.model.Explosion load data}.
	 * @param next statement to be evaluated under a heavy load.
     * @param data data that defines the load under which a statement
     * @param currentTestMethod actual test being run
     */
	public FailOnLoadExplosion(Statement next, List<Explosion> data, Description currentTestMethod){
        super(next);
        this.currentTestMethod  = currentTestMethod;
		measure                 = new Measure(0L, 0L, 0L);
		cluster 			    = data;
	}

    @Override
    protected void capture(long min, long max, long avg){
		measure = new Measure(max, min, avg);
        get().register(currentTestMethod, new Registry.ExecutionRecord(PerformanceKey.LOAD, measure));
	}

    // implements Statement#evaluate():void
	@Override
    public void evaluate() throws Throwable {
		for(int idx = 0; idx < 1000; idx ++){}   // warm up

		for(Explosion each : cluster){
            explode(each, false);
		}
	}


	@Override
    public Measure toMeasure(){
		return new Measure(measure);
	}


}
